var Sensors = new Class({
    sensorBuffer: {},
    orientationValue: false,
    motionValue: false,
    initialize: function() {
        window.addEvent("domready", function() {
            document.id(document.body).addEvent("touchstart", function(e) {
                e.stop();
            });
        });
        var addSocketEvents = (function() {
            socket.addEvents({
                message: (function(e) {
                    var message = e.data;
                    if(message.indexOf("$sensors:") == 0) {
                        var sensors = Array.from(JSON.decode(message.substr(9), true));
                        this.clear();
                        if(sensors && sensors.length) {
                            window.addEvent("domready", function() {
                                document.id(document.body).set("class", "ingame");
                            });
                            this.setOrientationEvent(sensors);
                        // this.setMotionEvent(sensors);
                        }
                    } else if(message == "/Unpaired") {
                        this.clear();
                    }
                }).bind(this),
                close: (function() {
                    this.clear();
                }).bind(this)
            });
        }).bind(this);
        addSocketEvents();
        window.addEvent("domready", (function() {
            document.id("reconnect").addEvent("touch", addSocketEvents);
        }).bind(this));
    },
    clear: function() {
        if(this.orientationEvent) {
            window.removeEventListener("deviceorientation", this.orientationEvent);
            this.orientationEvent = null;
            this.orientationValue = false;
        }
        if(this.motionEvent) {
            window.removeEventListener("devicemotion", this.motionEvent);
            this.motionEvent = null;
            this.motionValue = false;
        }
        this.sensorBuffer = {};
    },
    setOrientationEvent: function(sensors) {
        var alpha, beta, gamma;
        sensors.forEach(function(sensor) {
            if(sensor == "orientationAlpha") alpha = true;
            if(sensor == "orientationBeta") beta = true;
            if(sensor == "orientationGamma") gamma = true;
        });
        if(window.devicePixelRatio >= 2) { // Retina Display (& device with gyroscope)
            this.orientationEvent = (function(e) {
                if(alpha) this.sensorBuffer.orientationAlpha = e.alpha;
                if(beta) this.sensorBuffer.orientationBeta = e.beta;
                if(gamma) this.sensorBuffer.orientationGamma = e.gamma;
                if(!socket.bufferedAmount/* && !this.motionEvent || this.motionValue*/) {
                    socket.send(JSON.encode(this.sensorBuffer));
                }
            }).bind(this);
            window.addEventListener("deviceorientation", this.orientationEvent, false);
        } else if(gamma && !alpha && !beta) { // workaround for non gyroscope (only for gamma at the moment)
            this.orientationEvent = (function(e) {
                var x = e.accelerationIncludingGravity.x;
                var z = e.accelerationIncludingGravity.z;
                if(x < 6 && x > -6) {
                    if(z < 0) {
                        this.sensorBuffer.orientationGamma = x * 6;
                    } else {
                        this.sensorBuffer.orientationGamma = 180 - x * 6;
                    }
                } else {
                    if(x > 0) {
                    this.sensorBuffer.orientationGamma = 90 + z * 6;
                    } else {
                        this.sensorBuffer.orientationGamma = -90 - z * 6;
                    }
                }
                if(!socket.bufferedAmount/* && !this.motionEvent || this.motionValue*/) {
                    socket.send(JSON.encode(this.sensorBuffer));
                }
            }).bind(this);
            window.addEventListener("devicemotion", this.orientationEvent, false);
        } else {
            alert("Sorry, this game needs a gyroscope (only iPhone 4, iPod touch 4. generation and iPad 2 or later).");
        }
    },
    setMotionEvent: function(sensors) {
    // TODO not implemented
    }
});
var sensors = new Sensors();